"""
WSGI config for aiops_backend project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aiops_backend.settings")

application = get_wsgi_application()

import atexit

# from aiops_backend.settings import threading_list
from scripts.monitor_thread import MonitorThread

max_threading_count = 10
max_docker_count = 4
monitor_trd = None


def start_monitor_thread():
    global monitor_trd
    if not monitor_trd:
        monitor_trd = MonitorThread(max_threading_count, max_docker_count)
        print(f"moniteor_docker_nums: {monitor_trd.cur_docker_count}")
        monitor_trd.daemon = True
        print("Monitor thread start...")
        monitor_trd.start()


def stop_monitor_thread():
    global monitor_trd
    if monitor_trd and monitor_trd.is_alive():
        monitor_trd.stop()
    print("Monitor thread stop...")


# 注册退出函数
atexit.register(stop_monitor_thread)

start_monitor_thread()
